package Twitter
{
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	
	
	
	public class GroupActions
	{
		public var last_group:String = "";
		
		public var LOCK_global_group_members:Boolean = false;
		
		public function GroupActions()
		{
		}
		
		public function bind_group_titles():void
		{
			Application.application.t_db.getGroup(Application.application._username);
			
			Application.application.arr_group_titles = new ArrayCollection();
			
			
			Application.application.arr_group_titles.addItem("ALL GROUPS");
			
			for each (var o:Groups in Application.application.t_db._array_db_groups)
			{
				//Alert.show(o.group_id);
				Application.application.arr_group_titles.addItem(o.group_id);
				
			} 
			
			if (Application.application.arr_group_titles.length>0)
			{
				bind_global_group_members();
			}
			
			
			/*var groupsSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username);
			
			if (groupsSO!=null)
			{
				for each(var s:String in groupsSO.data.Groups)
				{
					Application.application.arr_group_titles.addItem(s);
					
				}
				
				
			}
			
			if (Application.application.arr_group_titles.length>0)
			{
				//bind_group_members();
			}*/
			
			//Alert.show("bind_group_titles");
			
			
			
			
			
			
		}
		
		public function create_group(group_id:String):Boolean
		{
			//check if group exists & insert
			
			return Application.application.t_db.insertGroup(group_id, Application.application._username);

			
			/*var groupsSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username);
			
			if (groupsSO!=null)
			{
				var GroupsArr:Array = groupsSO.data.Groups
				
				if (GroupsArr.indexOf(group_id)==-1)
				{
					GroupsArr.push(group_id);
					groupsSO.data.Groups = GroupsArr;
					
					//
					
					groupsSO.flush();
					
					return true;
				}
			
			} */
			
			//return false;
		}
		
		
		
		public function add_group_member(group_id:String, user_id:String):Boolean
		{
			//check if member exists
			
			if (Application.application.t_db.check_exists_GroupMember(user_id, group_id, Application.application._username)==false)
			{
				Application.application.t_db.insertGroupMember(user_id, group_id, Application.application._username);
				return true;
			
			}
			else
			{
				return false;	
			
			}
			/*var thisgroupSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username+"_"+group_id);
			
			if (thisgroupSO!=null)
			{
				
			
				if (thisgroupSO.data.members!=null)
				{
					var member:Array = thisgroupSO.data.members;
					if (member.indexOf(user_id)==-1)
					{
					member.push(user_id);
					member.sort();
					
					thisgroupSO.data.members = member;
					thisgroupSO.flush();
					
					return true;
					
					}
					
				}
				else
				{
					var member:Array = new Array();
					member.push(user_id);
					
					thisgroupSO.data.members = member;
					thisgroupSO.flush();
					
					return true;
					
				}
				
				
				
				
			}*/
			
			return false
			
		}
		
		public function get_group_members(group_id:String):void
		{
			//get group members
			Application.application.arr_group_members = null;
			
			Application.application.t_db.getGroupMember(group_id, Application.application._username);
			
			if (Application.application.t_db._array_db_group_members.length>0)
			{
				for each(var o:GroupMembers in Application.application.t_db._array_db_group_members)
				{
					
					Application.application.arr_group_members.addItem(o);
				}
			}
			
			/*var thisgroupSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username+"_"+group_id);
			
			if (thisgroupSO!=null)
			{
				for each (var u:String in thisgroupSO.data.members)
				{
					var o:GroupMembers = new GroupMembers();
					
					o.group_id = group_id;
					o.user_id = u;
					o.username = Application.application._username;
					
					Application.application.arr_group_members.addItem(o);
				}
				
			}*/
			
		}
		
		public function bind_global_group_members():void
		{
			//get global list
			
			if (LOCK_global_group_members == false)
			{
				LOCK_global_group_members = true;
				
				/*var groupsSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username);
			
				if (groupsSO!=null)
				{
					for each(var group_id:String in groupsSO.data.Groups)
					{
						var thisgroup:SharedObject = SharedObject.getLocal("groups_"+Application.application._username+"_"+group_id);
						
						for each (var user_id:String in thisgroup.data.members)
						{
							var o:GroupMembers = new GroupMembers();
							
							o.group_id = group_id;
							o.user_id = user_id;
							o.username = Application.application._username;
							
							Application.application.global_group_members.addItem(o);
							
						}
						
					}
					
					
				}*/
				
				
				
				//OLD DB CODE
				Application.application.global_group_members = new ArrayCollection();
				{
					
					Application.application.t_db.getGlobalGroupMember(Application.application._username);
					
					//Alert.show("Called bind_group_members()" + t_db._array_db_global_group_members.length);
					
					for each (var o:GroupMembers in Application.application.t_db._array_db_global_group_members)
					{
						//Alert.show(o.user_id);
						Application.application.global_group_members.addItem(o);
						
						
						
					} 
					
				}
			
			}
			
			LOCK_global_group_members = false
			
			
		}
		
		public function delete_group_member(group_id:String, user_id:String):Boolean
		{
			//delete user
			
			Application.application.t_db.deleteGroupMember(group_id, user_id, Application.application._username);
			
			return true;
			
			
			/*var thisgroupSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username+"_"+group_id);
			
			if (thisgroupSO!=null)
			{
				
			
				if (thisgroupSO.data.members!=null)
				{
					var member:Array = thisgroupSO.data.members;
					if (member.indexOf(user_id)>-1)
					{
						var i:int = member.indexOf(user_id);
						member.splice(i,1);
						member.sort();
					
						thisgroupSO.data.members = member;
						thisgroupSO.flush();
					
					return true;
					
					}
					
				}
				else
				{
					var member:Array = new Array();
					member.push(user_id);
					
					thisgroupSO.data.members = member;
					thisgroupSO.flush();
					
					return true;
					
				}
				
				
				
				
			}*/
			
			return false
			
			
		}
		
		public function delete_group(group_id:String):void
		{
			Application.application.t_db.deleteGroup(group_id, Application.application._username);
			// delete group
			
			// delete all users in group
			
			
			/*var groupsSO:SharedObject = SharedObject.getLocal("groups_"+Application.application._username);
			
			
			
			if (groupsSO.data.Groups!=null)
			{
				var groupArr:Array = groupsSO.data.Groups;
				if (groupArr.indexOf(group_id)>-1)
				{
					if (delete_group_file(group_id)==true)
					{
						var i:int = groupArr.indexOf(group_id);
						groupArr.splice(i,1);
						
						groupsSO.data.Groups = groupArr;
						groupsSO.flush();
					
					}
					
					
				}
				
			}*/
			
			
		}
		

}
}